#!/usr/bin/perl -w  # useful for syntax highlighting!
package clreqd_modules;

use strict;
use cldb;
use Data::Dumper;
use Config;
use clutils qw(to_bool);

sub add_clevent {
my %ARGS=@_;
my $rc;

	my @E=qw(net.up net.down net.migrate node.daemon_error node.daemon_warn node.join node.leave node.down app.start app.end app.fail app.monitor node.monitor app.net_route);
	my @T=qw(email log socket cmd);
	my %V=(
		"email"	=> ["EMAIL"],
		"log"	=> ["FILE"],
		"cmd"	=> ["CMD"],
		"socket"=> ["HOST","PORT"],
		);

	my $NAME=$ARGS{NAME};
	my $EVENT=lc($ARGS{EVENT});
	my $TEXT=$ARGS{TEXT};
	my $TYPE=lc($ARGS{TYPE});

	#################################################################
	# Optional variables ...					#
	#################################################################
	my ($HOST,$PORT,$EMAIL,$FILE,$CMD,$REPLACE);
	$REPLACE=0;
	$HOST=$ARGS{HOST} if exists($ARGS{HOST});
	$PORT=$ARGS{PORT} if exists($ARGS{PORT});
	$EMAIL=$ARGS{EMAIL} if exists($ARGS{EMAIL});
	$FILE=$ARGS{FILE} if exists($ARGS{FILE});
	$CMD=$ARGS{CMD} if exists($ARGS{CMD});
	$REPLACE=to_bool($ARGS{REPLACE}) if exists($ARGS{REPLACE});

	#################################################################
	# Check to see if the EVENT is a known event and abort with	#
	# error if not.							#
	#################################################################

	if(! grep {$_ eq $EVENT} @E) {
		return "ERROR Unknown event '$EVENT' specified.";
	}
	if(! grep {$_ eq $TYPE} @T) {
		return "ERROR Unknown event type '$TYPE' specified.";
	}

	#################################################################
	# Check required variables for event type specified.		#
	#################################################################

	my $ll=$V{$TYPE};
	my @m=();
	for my $c (@$ll) {
		if(!exists($ARGS{$c}) || !defined($ARGS{$c})) {
			push @m,$c;
		}
	}
	if(@m) {
		return "ERROR Missing mandatory parameters: " . join(",",@m);
	}

	#################################################################
	# We have all the data we need now check to see if any		#
	# record of this type exists, and abort if it does and 		#
	# replace is not set, otherwise delete it ready for adding.	#
	#################################################################

	my $CLDB=new cldb("$::ENV{TRUECL_CFG}/config.db");
	if(!defined($CLDB)) {
		return "ERROR Unable to config to 'config' database.";
	}
	my $res=$CLDB->get_rows("SELECT * FROM cluster_events WHERE name = '$NAME';");
	if(defined($res->[0]->{name})) {
		if($REPLACE==0) {
			return "EXISTS";
		}
		#########################################################
		# Exists but we are allowed to replace it .		#
		#########################################################
	}

	#################################################################
	# Now we can add the record, so build up the required cmd.	#
	#################################################################

	my $rec="";
	if($TYPE eq "email") {
		$rec="INSERT OR REPLACE INTO cluster_events (name,event,text,type,send_email) VALUES ('$NAME','$EVENT','$TEXT','$TYPE','$EMAIL');"
	}
	if($TYPE eq "log") {
		$rec="INSERT OR REPLACE INTO cluster_events (name,event,text,type,send_file) VALUES ('$NAME','$EVENT','$TEXT','$TYPE','$FILE');"
	}
	if($TYPE eq "cmd") {
		$rec="INSERT OR REPLACE INTO cluster_events (name,event,text,type,send_cmd) VALUES ('$NAME','$EVENT','$TEXT','$TYPE','$CMD');"
	}
	if($TYPE eq "socket") {
		$rec="INSERT OR REPLACE INTO cluster_events (name,event,text,type,send_host,send_port) VALUES ('$NAME','$EVENT','$TEXT','$TYPE','$HOST','$PORT');"
	}

	#################################################################
	# Now insert the record ....					#
	#################################################################
	$res=$CLDB->do($rec);
	if(!$res) {
		my ($err_rc,$err_str)=cldb::error();
		return "ERROR Failed to write to config database: $err_str [RC=$err_rc]";
	}
	return "OK";
}

1;

